﻿using System;
using System.Windows.Forms;
using HighDiveEngine.Resources;
using HighDiveEngine.Model;

namespace HighDiveAdmin.GUI.Components
{
    public partial class EditContestJudgesPanel : UserControl
    {
        public event DelegateVoidInt EventUpdateJudge;
        public event DelegateVoidInt EventSetNumberOfJudges;
        public event MethodInvoker EventNewJudge;
        public EditContestJudgesPanel()
        {
            InitializeComponent();
        }

        private void textBoxJudge_Click(object sender, EventArgs e)
        {            
            var tb = (TextBox) sender;
            int index = 0;
            switch (tb.Name)
            {
                case "textBoxJudge1":index = 1;break;
                case "textBoxJudge2":index = 2;break;
                case "textBoxJudge3":index = 3;break;
                case "textBoxJudge4":index = 4;break;
                case "textBoxJudge5":index = 5;break;
                case "textBoxJudge6":index = 6;break;
                case "textBoxJudge7":index = 7;break;
            }
            Visible = false;

            if (EventUpdateJudge != null)
                EventUpdateJudge(index);            
        }

        private void numericUpDownNumberofJudges_ValueChanged(object sender, EventArgs e)
        {
            if (numericUpDownNumberofJudges.Value == 4 || numericUpDownNumberofJudges.Value == 6)
                numericUpDownNumberofJudges.Value = 5;
            if (EventSetNumberOfJudges != null)
                EventSetNumberOfJudges(Convert.ToInt32(numericUpDownNumberofJudges.Value));
        }

        public void FillJudgeBox(Judge judge, int number)
        {
            switch (number)
            {
                case 1: textBoxJudge1.Text = judge.ToString();break;
                case 2: textBoxJudge2.Text = judge.ToString(); break;
                case 3: textBoxJudge3.Text = judge.ToString(); break;
                case 4: textBoxJudge4.Text = judge.ToString(); break;
                case 5: textBoxJudge5.Text = judge.ToString(); break;
                case 6: textBoxJudge6.Text = judge.ToString(); break;
                case 7: textBoxJudge7.Text = judge.ToString(); break;
            }
        }

        internal void FillJudges(Contest contest)
        {
            foreach (var judge in contest.JudgeList)
            {
                switch (judge.JudgeNumber)
                {
                    case 1: textBoxJudge1.Text = judge.ToString(); break;
                    case 2: textBoxJudge2.Text = judge.ToString(); break;
                    case 3: textBoxJudge3.Text = judge.ToString(); break;
                    case 4: textBoxJudge4.Text = judge.ToString(); break;
                    case 5: textBoxJudge5.Text = judge.ToString(); break;
                    case 6: textBoxJudge6.Text = judge.ToString(); break;
                    case 7: textBoxJudge7.Text = judge.ToString(); break;
                }
            }
        }

        private void SetJudgeBoxes( int num)
        {
            int index = 1;
            foreach (Control control in groupBoxJudges.Controls)
            {
                var tbox = control as TextBox;
                if (tbox != null && index <= num)
                {
                    tbox.Visible = true;
                    tbox.Text = "Add Judge";
                    index++;
                }
                else if (tbox != null && index > num)
                {
                    tbox.Visible = false;
                    index++;
                }
            }
            index = 1;
            foreach (Control control in groupBoxJudges.Controls)
            {
                var lbl = control as Label;
                if (lbl != null && index <= num && lbl.Name != "labelNumberofJudges")
                {
                    lbl.Visible = true;
                    index++;
                }
                else if (lbl != null && index > num && lbl.Name != "labelNumberofJudges")
                {
                    lbl.Visible = false;
                    index++;
                }
            }
        }

        internal void Show(HighDiveEngine.Aggregates.JudgeList judgeList)
        {
            SetJudgeBoxes(judgeList.NumberOfJudges);
            numericUpDownNumberofJudges.Value = judgeList.NumberOfJudges;
            foreach (var judge in judgeList)
            {
                switch (judge.JudgeNumber)
                {
                    case 1: textBoxJudge1.Text = judge.ToString(); break;
                    case 2: textBoxJudge2.Text = judge.ToString(); break;
                    case 3: textBoxJudge3.Text = judge.ToString(); break;
                    case 4: textBoxJudge4.Text = judge.ToString(); break;
                    case 5: textBoxJudge5.Text = judge.ToString(); break;
                    case 6: textBoxJudge6.Text = judge.ToString(); break;
                    case 7: textBoxJudge7.Text = judge.ToString(); break;
                }
            }
            Visible = true;
        }

        #region TabStop
        private void EditContestJudgesPanel_TabStopChanged(object sender, EventArgs e)
        {
            foreach (Control control in Controls)
                control.TabStop = TabStop;
            if(TabStop)
                numericUpDownNumberofJudges.Focus();
        }

        private void EditContestJudgesPanel_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
                numericUpDownNumberofJudges.Focus();
        }
        #endregion

        private void textBoxJudge_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData.HasFlag(Keys.Enter))
                textBoxJudge_Click(sender, null);
        }
    }
}
